from pyflink.common import WatermarkStrategy, Encoder, Types
from pyflink.datastream import StreamExecutionEnvironment, RuntimeExecutionMode
from pyflink.datastream.connectors import (FileSource, StreamFormat, FileSink, OutputFileConfig,
                                           RollingPolicy)

if __name__ == '__main__':
    # 1、创建环境
    env = StreamExecutionEnvironment.get_execution_environment()

    # 2、读取数据
    lines_ds = env.read_text_file("words01.txt")

    # 统计单词的数量
    words_ds = lines_ds.flat_map(lambda line: line.split(","))

    kv_ds = lines_ds.map(lambda word: (word, 1))

    ky_by_ds = kv_ds.key_by(lambda kv: kv[0])

    count_ds = ky_by_ds.sum(1)

    count_ds.print()

    env.execute("wc")
